package com.leetcode.Offer;

import java.util.ArrayList;

public class JZ57_II {
    public int[][] findContinuousSequence(int target) {

        //滑动窗口
        int start = 1;//左边界
        int end = 1;//右边界
        int sum = 0;//窗口和

        ArrayList<int[]> ints = new ArrayList<>();//用于记录结果集

        while (start <= target / 2) {
            sum = (start + end) * (end - start + 1) / 2;
            if (sum < target) {
                end++;
            }
            if (sum > target) {
                start++;
            }
            if (sum == target) {
                int[] tmp = new int[end - start + 1];
                int a = 0;
                for (int i = start; i <= end; i++) {
                    tmp[a++] = i;
                }
                ints.add(tmp);
                //成功找到后对start和end初始化
                start++;
                end = start;
            }
        }
        return ints.toArray(new int[ints.size()][]);
    }

    public static void main(String[] args) {
        for (int[] ints : new JZ57_II().findContinuousSequence(1)) {
            for (int anInt : ints) {
                System.out.print(anInt + ",");
            }
            System.out.println();
        }
    }
}
